#!/bin/bash

mount_point=$(test -z "$1" && echo "/tmp/ntfs" || echo "$1")

# mount point
if [ -d $mount_point ]; then
    item_count=$(ls -A $mount_point | wc -l)
    if [ $item_count -gt 0 ]; then
        echo "directory $mount_point is not empty"
        mount_point="$mount_point/ntfs"
        mkdir $mount_point
    fi
else
    mkdir $mount_point
fi

# target ntfs device
device_name=$(diskutil list | grep Windows_NTFS | grep -oE "disk\S*")

echo "mount point: $mount_point"
echo "device: $device_name"

mounted_count=$(df | grep "^/dev/$device_name" | wc -l)
if [ $mounted_count -gt 0 ]; then
    echo -n "device mounted already. umount it... "
    sudo umount /dev/$device_name
    echo "unmounted."
fi

sudo mount -t ntfs -o rw,nobrowse /dev/$device_name $mount_point
echo "ntfs device:$device_name mounted to $mount_point"
